<?php
class Grabber {

    private $receiver_url;
    private $user;
    private $password;
    private $incoming_dir;
    private $sent_dir;
    private $suspect_dir;
    
    function __construct()
    {
        $this->loadConfig();
    }

    private function loadConfig()
    {
        require_once "config/config.php";
        $this->receiver_url = $receiver_url;
        $this->user = $user;
        $this->password = $password;
        $this->incoming_dir = $incoming_dir;
        $this->sent_dir = $sent_dir;
        $this->suspect_dir = $suspect_dir;

        if (!is_dir($this->incoming_dir)) {
            $this->error("Incoming directory doesn't exists\n");
        }

        if (!is_dir($this->sent_dir)) {
            $this->error("Sent directory doesn't exists\n");
        }

        if (substr($this->incoming_dir, 0, -1) != '/') {
            $this->incoming_dir .= '/';
        }

        if (substr($this->sent_dir, 0, -1) != '/') {
            $this->sent_dir .= '/';
        }
        
        if (substr($this->suspect_dir, 0, -1) != '/') {
            $this->suspect_dir .= '/';
        }
    }

    function connect()
    {
    }

    function process()
    {
        // Check incoming
        
        if (!$adr = scandir($this->incoming_dir)) {
            $this->error("Can't read incoming directory");
        }
        
        foreach ($adr as $file) {
            if ($file{0} != '.') {
                if (eregi("\.png$|\.jpg$|\.jpeg$|\.gif$", $this->incoming_dir . $file)) {
                    print "Processing $file ...\n";
                    $sender = file_get_contents($this->incoming_dir . $file.'.txt');
                    $this->postImage($this->incoming_dir.$file, $sender);

                    $sentdir =  $this->sent_dir . date('Ymd') . '/';
                    if (!is_dir($sentdir)) {
                        if (!@mkdir($sentdir, 0755)) {
                            $this->error("Can't create sent directory");
                        }
                        chmod($sentdir, 0755);
                    }
                    // Move image file to sent
                    copy($this->incoming_dir.$file, $sentdir.$file);
                    unlink($this->incoming_dir.$file);
                    copy($this->incoming_dir.$file.'.txt', $sentdir.$file.'.txt');
                    unlink($this->incoming_dir.$file.'.txt');
                } else {
                    $bad = false;
                    // Check if is txt def.
                    if (!eregi("\.png.txt$|\.jpg.txt$|\.jpeg.txt$|\.gif.txt$", $this->incoming_dir . $file)) {
                        //Move 'not image' fle to suspects bad behaviour
                        copy($this->incoming_dir.$file, $this->suspect_dir.$file);
                        unlink($this->incoming_dir.$file);
                    }
                }
            }
        }
    }

    private function postImage($file, $sender) 
    {
        print "Post to " . $this->receiver_url . "\n";
        $ch = curl_init($this->receiver_url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST, 1);

        $postFields = array(
            'file'     => '@'.$file,
            'username' => $this->user,
            'password' => $this->password,
            'sender'   => $sender
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

        $buffer = curl_exec($ch);
        print "Result: <pre>$buffer</pre>\n";
        curl_close($ch);
    }

    private function error($msg) 
    {
        print "Error $msg\n";
        die;
    }
    
}
